﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MedicalManagementSystem.Models;
using MedicalManagementSystem.Utils;
using MedicalManagementSystem.Helpers;

namespace MedicalManagementSystem.Controllers
{
    public class DiagnosBlocController : GenericController
    {
        //
        // GET: /DiagnosBloc/
        [EnumAuthorize(Roles = UserRoles.DoctorRole | UserRoles.StatisticRole)]
        public ActionResult Index()
        {
            Menu.ID = "adm,diagnos";
            return View(DbContext.Instance.DiagnosBloc);
        }

        //
        // GET: /DiagnosBloc/Details/5
        [EnumAuthorize(Roles = UserRoles.DoctorRole | UserRoles.StatisticRole)]
        public ActionResult Details(int id)
        {
            return RedirectToAction("Index", "Diagnos", new {id});
        }

        //
        // GET: /DiagnosBloc/Create
        [EnumAuthorize(Roles = UserRoles.DoctorRole )]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /DiagnosBloc/Create
        [EnumAuthorize(Roles = UserRoles.DoctorRole )]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "id")]DiagnosBloc diagnosBloc)
        {
            if (diagnosBloc.CheckBeforeSave())
            {
                DbContext.Instance.AddToDiagnosBloc(diagnosBloc);
                DbContext.Instance.SaveChanges();

                return RedirectToAction("Index");
            }
            else
            {
                AddViolataionsToModelState(diagnosBloc.GetRuleViolations());
            }

            return View(diagnosBloc);
        }
        
        //
        // GET: /DiagnosBloc/Edit/5
        [EnumAuthorize(Roles = UserRoles.DoctorRole )]
        public ActionResult Edit(int id)
        {
            var diagnosBloc = DiagnosBloc.GetByPK(id);
            return View(diagnosBloc);
        }

        //
        // POST: /DiagnosBloc/Edit/5
        [EnumAuthorize(Roles = UserRoles.DoctorRole)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, [Bind] DiagnosBloc diagnosBloc)
        {
            if (diagnosBloc.CheckBeforeSave())
            {
                var original = DiagnosBloc.GetByPK(id);
                DbContext.Instance.ApplyPropertyChanges(original.EntityKey.EntitySetName, diagnosBloc);
                DbContext.Instance.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                AddViolataionsToModelState(diagnosBloc.GetRuleViolations());
            }
            return View(diagnosBloc);
        }

        //
        // POST: /DiagnosBloc/Delete/5
        [EnumAuthorize(Roles = UserRoles.DoctorRole )]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Delete(int id)
        {
            try
            {
                var diagnosBloc = DiagnosBloc.GetByPK(id);
                DbContext.Instance.DeleteObject(diagnosBloc);
                DbContext.Instance.SaveChanges(true);
            }
            catch (Exception e)
            {

            }

            return RedirectToAction("Index");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult GetListOfDiagnosis(int blockId)
        {
            var diagnosList = DiagnosBloc.GetByPK(blockId)
                .Do(b => b.Diagnos.Load()).Diagnos
                .Select(d => new KeyValuePair<int, string>(d.id, d.Name))
                .OrderBy(d=>d.Key);
            return Json(diagnosList);
        }
    }
}
